package cn.sdhuijin.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class CommonUtil {

	private static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 格式化日期类型
	 * 
	 * @param date
	 *            数据库日期
	 * @return 格式化后日期(yyyy-MM-dd HH:mm:ss)
	 */
	public static String formatDate(Date date) {
		DateFormat format = null;
		String formatDate = "";
		try {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			formatDate = format.format(date);
		} catch (Exception e) {

		}
		return formatDate;
	}

	/**
	 * 判断字符串数组中的所有字符串是否为空
	 *
	 * @param str
	 *            字符串数组
	 * @return 字符串都不为空，返回true；否则，返回false
	 */
	public static boolean allIsNotEmpty(String... str) {
		Boolean flag = true;
		for (String s : str) {
			if (s == null || s.trim().equals("")) {
				flag = false;
			}
		}
		return flag;
	}

	/**
	 * String convert to InputStream
	 * 
	 * @param str
	 * @return
	 */
	public static InputStream getInputStreamFromString(String str) {
		InputStream in = new ByteArrayInputStream(str.getBytes());
		return in;
	}

	/**
	 * 生成六位随机数字
	 * 
	 * @return
	 */
	public static String GenerateSixRandomNum() {
		Random random = new Random();
		int x = random.nextInt(899999);
		x = x + 100000;
		return String.valueOf(x);
	}

	/**
	 * 在一定范围内生成随机数
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int GenerateRandomNum(int min, int max) {
		int s = 0;
		if (min < max) {
			Random random = new Random();
			s = random.nextInt(max) % (max - min + 1) + min;
		}
		return s;
	}

	/**
	 * <b>function:</b>json字符串转换成list<map>
	 */
	public static List<LinkedHashMap<String, Object>> readJson2List(String json) {
		List<LinkedHashMap<String, Object>> list = null;
		try {
			list = objectMapper.readValue(json, List.class);
			// for (int i = 0; i < list.size(); i++) {
			// Map<String, Object> map = list.get(i);
			// Set<String> set = map.keySet();
			// }
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 随机生成颜色代码
	 */
	public static String getRandColorCode() {
		String r, g, b;
		Random random = new Random();
		r = Integer.toHexString(random.nextInt(256)).toUpperCase();
		g = Integer.toHexString(random.nextInt(256)).toUpperCase();
		b = Integer.toHexString(random.nextInt(256)).toUpperCase();

		r = r.length() == 1 ? "0" + r : r;
		g = g.length() == 1 ? "0" + g : g;
		b = b.length() == 1 ? "0" + b : b;

		return "#" + r + g + b;
	}

	/**
	 * 格式化日期类型
	 * 
	 * @param date
	 *            数据库日期
	 * @return 格式化后日期(yy/MM/dd)
	 */
	public static String formatDateToYmdString(Date date) {
		DateFormat format = null;
		String formatDate = "";
		try {
			format = new SimpleDateFormat("yy/MM/dd");
			formatDate = format.format(date);
		} catch (Exception e) {

		}
		return formatDate;
	}

	/**
	 * 计算百分比
	 * 
	 * @param x
	 * @param total
	 * @return
	 */
	public static Double getPercent(Double x, Double total) {
		double tempresult = x / total;
		DecimalFormat df1 = new DecimalFormat("0.00"); // ##.00%
														// 百分比格式，后面不足2位的用0补齐
		return Double.parseDouble(df1.format(tempresult));
	}
}
